import axios from 'axios'
import store from '../store'
import {USER_KEY} from '../store/userReducer'

const instance = axios.create({
    baseURL: 'https://shop.fed.lagou.com/api/admin'
})
instance.interceptors.request.use(config => {
    const user = store.getState()[USER_KEY].user
    if (user && user.token) {
        if (!config.headers) {
            config.headers = {}
        }
        config.headers.Authorization = `Bearer ${user.token}`
    }
    return config
}, err => {
    return Promise.reject(err)
})
// instance.interceptors.response.use(res => {
//   console.log(res.data.status)
//   if (res.data.status === 410000) {
//     store.dispatch(removeUser())
//     console.log('重新')
//   }
//   return res
// }, err => {
//   return Promise.reject(err)
// })

const request = config => instance(config).then(res => res.data)

request.get = (url, params) => instance.get(url, {
    params
}).then(res => res.data)

request.post = (url, body) => instance.post(url, body).then(res => res.data)



export const getProduct = (params) => {
    return request.get('/product/product', params)
}


export default request